﻿Namespace Security
    Friend Class FEditSecurityGroup
        Private mGroup As Entities.SecurityGroup

        Public Shadows Function ShowDialog(ByVal owner As Form, ByVal group As Entities.SecurityGroup, ByVal options As EditGroupOptions) As DialogResult
            mGroup = group

            Me.Text = options.Caption
            txtName.Text = group.Name
            txtDescription.Text = group.Description
            txtType.Text = group.Type.ToString
            txtSubType.Text = group.SubType.ToString

            If options.EditMembersAvailable Then
                For Each clsBuddy As Entities.Buddy In Handler.GetAllMembers(group.Id)
                    UpdateBuddy(clsBuddy, True)
                Next
            End If

            AdjustGUI(group, options)

            Return MyBase.ShowDialog(owner)
        End Function

        Private Sub AdjustGUI(ByVal group As Entities.SecurityGroup, ByVal options As EditGroupOptions)
            Dim iTotal As Integer = Me.Height - Me.ClientSize.Height + Panel1.Padding.Top + Panel1.Padding.Bottom
            iTotal += gpProperties.Height
            iTotal += pnlButtons.Height
            If options.EditMembersAvailable Then
                iTotal += gbGroupMembership.Height
            Else
                gbGroupMembership.Visible = False
            End If
            Me.Height = iTotal
            If group.Type = Entities.SecurityGroupType.System Then
                btnAdd.Visible = False
                btnRemove.Visible = False
                btnEdit.Visible = False
                lvwBuddies.Width = btnAdd.Right - lvwBuddies.Left
                txtName.ReadOnly = True
                txtDescription.ReadOnly = True
                btnOK.Visible = False
                btnCancel.Text = "Close"
            End If
        End Sub

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Try
                mGroup.Name = txtName.Text
                mGroup.Description = txtDescription.Text
                Using t As New Data.TransactionScope
                    Security.Handler.Save(mGroup, t)
                    Security.Handler.SetGroupMembers(mGroup.Id, GetBuddyIds.ToList, t)
                    t.Complete()
                End Using
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub

        Private Sub UpdateBuddy(ByVal buddy As Entities.Buddy, ByVal addIfMissing As Boolean)
            Dim strKey As String = buddy.Id.ToString
            If lvwBuddies.Items.ContainsKey(strKey) Then
                With lvwBuddies.Items(strKey)
                    .Text = buddy.NameLocal
                    .Tag = buddy
                End With
            ElseIf addIfMissing Then
                With lvwBuddies.Items.Add(strKey, buddy.NameLocal, CStr(Nothing))
                    .Tag = buddy
                End With
            End If
        End Sub
        Private Sub EventHandler_BuddyUpdated(ByVal buddy As Entities.Buddy)
            Try
                UpdateBuddy(buddy, False)
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
        Private Sub FEditSecurityGroup_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            RemoveHandler Framework.Buddies.Manager.UpdatedGUI, AddressOf EventHandler_BuddyUpdated
        End Sub
        Private Sub FEditSecurityGroup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            AddHandler Framework.Buddies.Manager.UpdatedGUI, AddressOf EventHandler_BuddyUpdated
        End Sub

        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Try
                Dim clsOpts As New Framework.Buddies.SelectBuddiesOptions
                clsOpts.Caption = String.Format("Add buddies to group {0}", txtName.Text)
                clsOpts.HideBuddyIds = GetBuddyIds()
                For Each clsBud As Entities.Buddy In Framework.Buddies.Manager.SelectBuddies(Me, clsOpts)
                    UpdateBuddy(clsBud, True)
                Next
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Function GetBuddyIds() As Generic.HashSet(Of Integer)
            Dim aRet As New Generic.HashSet(Of Integer)
            For Each clsItem As ListViewItem In lvwBuddies.Items
                aRet.Add(CType(clsItem.Tag, Entities.Buddy).Id)
            Next
            Return aRet
        End Function

        Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
            Try
                While lvwBuddies.SelectedItems.Count > 0
                    lvwBuddies.SelectedItems(0).Remove()
                End While
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub EventHandler_EditBuddy(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click, lvwBuddies.DoubleClick
            Try
                If lvwBuddies.SelectedItems.Count = 1 Then
                    Dim clsBud As Entities.Buddy = CType(lvwBuddies.SelectedItems(0).Tag, Entities.Buddy)
                    Dim clsOpts As New Buddies.EditBuddyOptions
                    clsOpts.AllowGroupMembershipEdit = False
                    Buddies.Manager.EditBuddy(Me, clsBud, clsOpts)
                End If
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub lvwBuddies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwBuddies.SelectedIndexChanged
            btnEdit.Enabled = lvwBuddies.SelectedItems.Count = 1
            btnRemove.Enabled = lvwBuddies.SelectedItems.Count > 0
        End Sub
    End Class
End Namespace